﻿using System;
using System.Collections.Generic;


using We.Moonlight.Geometry;
using We.Moonlight.ViItem;

namespace We.Moonlight.MSql
{
    public static class SqlViItem
    {
        public static mDBTableAdapters.DocumentsTableAdapter DTA = new We.Moonlight.mDBTableAdapters.DocumentsTableAdapter();
        public static mDBTableAdapters.Documents1TableAdapter DTA1 = new We.Moonlight.mDBTableAdapters.Documents1TableAdapter();
        private static mDB.DocumentsDataTable DT = new mDB.DocumentsDataTable();

        public static ViDoc Select(int ID)
        {
            DTA.FillByID(DT, ID);
            if (DT.Rows.Count != 0)
            {
                return new ViDoc((mDB.DocumentsRow)DT.Rows[0]);
            }
            else
            {
                return null;
            }
        }
        public static IVi SelectLast(ViType VT)
        {
            return new ViDoc((mDB.DocumentsRow)DTA.GetLastREcordByType((int)VT).Rows[0]);
        }
        public static IVi SelectLast()
        {
            return new ViDoc((mDB.DocumentsRow)DTA.GetLastRecord().Rows[0]);
        }
        public static List<IVi> SelectDesk(int DeskID)
        {
            if (DTA.GetDataByDesk(DeskID).Rows.Count != 0)
            {
                List<IVi> L = new List<IVi>();
                foreach (mDB.DocumentsRow DR in DTA.GetDataByDesk(DeskID).Rows)
                {
                    L.Add(new ViDoc(DR));
                }
                return L;
            }
            else
            {
                return null;
            }
        }
        public static void Insert(IVi Doc, int DeskID)
        {
            DTA.Insert(DeskID, Doc.Location.X, Doc.Location.Y, Doc.Rotation, Doc.ScaleFactor, Doc.Flipped.ToInt32(), Doc.Name, Doc.Layer, (int)Doc.Type, Doc.File.FullName);
        }
        public static void Insert(int DeskID, int LX, int LY, double Rot, double Scl, bool flppd, string name, int layer, ViType typ, string Path)
        {
            DTA.Insert(DeskID, LX, LY, Rot, Scl, flppd.ToInt32(), name, layer, (int)typ, Path);
        }
        public static void Update(IVi D)
        {
            DTA.Update(D.DeskID, D.Location.X, D.Location.Y, D.Rotation, D.ScaleFactor, D.Flipped.ToInt32(), D.Name, D.Layer,(int)D.Type, D.File == null ? "" : D.File.FullName, D.ID);
        }
        public static void Update(IVi D, int ID)
        {
            DTA.Update(D.DeskID, D.Location.X, D.Location.Y, D.Rotation, D.ScaleFactor, D.Flipped.ToInt32(), D.Name, D.Layer, (int)D.Type, D.File.FullName, ID);
        }
        public static void Delete(IVi D)
        {
            DTA.Delete(D.ID);
        }
        public static void Delete(int ID)
        {
            DTA.Delete(ID);
        }
        public static void DeleteDesk(int DeskID)
        {
            DTA1.Delete(DeskID);
        }
    }
}